package com.example.timmy.myapplication.adapter;

import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.model.MultipleItem;

/**
 * 局部更新adapter的页面
 */

public class NotifyItemAdapter extends BaseQuickAdapter<MultipleItem, BaseViewHolder> {

    public NotifyItemAdapter() {
        super(null);

        //Step.1
        setMultiTypeDelegate(new MultiTypeDelegate<MultipleItem>() {
            @Override
            protected int getItemType(MultipleItem entity) {
                //根据你的实体类来判断布局类型
                return entity.getItemType();
            }
        });

        //Step.2
        getMultiTypeDelegate()
                .registerItemType(MultipleItem.TYPE_CORRENT, R.layout.item_notifyitem_corrent)
                .registerItemType(MultipleItem.TYPE_ERROR, R.layout.item_notifyitem_error);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, MultipleItem multipleItem) {

        int count = multipleItem.getCount();

        //Step.3
        int itemViewType = baseViewHolder.getItemViewType();

        Log.i(TAG, "convert: ");
        switch (itemViewType) {
            case MultipleItem.TYPE_CORRENT:
                // do something
                baseViewHolder.setText(R.id.tv_title, getFormatmillisecond(count));
                break;
            case MultipleItem.TYPE_ERROR:
                // do something
                baseViewHolder.setText(R.id.tv_title, getFormatmillisecond(count));
                break;
        }
    }


    public static String getFormatmillisecond(long time) {

        long millisecond = time % 1000;
        long second = (time / 1000) % 60;
        long minute = time / 1000 / 60;

        //秒以下的只显示一位
        String strMillisecond = millisecond / 10 + "";

        if (strMillisecond.length() == 1) {
            strMillisecond = "0" + strMillisecond;
        }

        //秒显示两位
        String strSecond = ("00" + second).substring(("00" + second).length() - 2);
        //分显示两位
        String strMinute = ("00" + minute).substring(("00" + minute).length() - 2);

        return strMinute + ":" + strSecond + "." + strMillisecond;
    }
}
